package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.base.BaseActivity;

/**
 * Created by Administrator on 2017/7/15 0015.
 */

public class CommonWvActivity extends BaseActivity implements View.OnClickListener{

    private ImageView common_wv_back;
    private TextView common_wv_title,common_wv_finsh;
    private WebView common_wv;
    private ProgressBar common_wv_progressBar;

    private String url,title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_wv);
        initView();
    }

    private void initView(){
        url = getIntent().getStringExtra("url");
        //title = getIntent().getStringExtra("title");
        common_wv_title = (TextView) findViewById(R.id.common_wv_title);
        common_wv_back = (ImageView) findViewById(R.id.common_wv_back);
        common_wv_back.setOnClickListener(this);
        common_wv_progressBar = (ProgressBar) findViewById(R.id.common_wv_progressBar);
        common_wv = (WebView) findViewById(R.id.common_wv);
        common_wv_finsh = (TextView) findViewById(R.id.common_wv_finsh);
        common_wv_finsh.setVisibility(View.GONE);
        //common_wv_title.setText(title);
        common_wv.getSettings().setJavaScriptEnabled(true);
        //设置滚动条可见
        setProgressBarIndeterminateVisibility(true);
        init();
        //Log.e("info","url="+url);
        common_wv.loadUrl(url);
    }

    private void init() {
        // TODO 自动生成的方法存根
        common_wv.setWebViewClient(new WebViewClient(){
            //覆写shouldOverrideUrlLoading实现内部显示网页
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO 自动生成的方法存根
                //view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                String title=view.getTitle();
                if(title==null){
                    title="";
                }
                common_wv_title.setText(title);
                super.onPageFinished(view, url);
            }
        });
        common_wv.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO 自动生成的方法存根
                common_wv_progressBar.setVisibility(View.VISIBLE);
                common_wv_progressBar.setProgress(newProgress);
                if (newProgress == 100) {
                    common_wv_progressBar.setVisibility(View.GONE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && common_wv.canGoBack()) {
            common_wv.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.common_wv_back:
                if (common_wv.canGoBack()){
                    common_wv.goBack();// 返回前一个页面
                }else {
                    CommonWvActivity.this.finish();
                }
                //CommonWvActivity.this.finish();
                break;
        }
    }

}
